<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
// import { createRequire } from "module";
// const require = createRequire(import.meta.url);
// console.log(require, "require");
import * as echarts from "echarts";
import("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
let chart = null
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      // chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!chart) {
      return;
    }
    chart.dispose();
    chart = null;
  },
  methods: {
    initChart() {
      chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData } = {}) {

      chart.setOption({
        title: {
          left: 'center',
          text: '七天内销售商品'
        },
        xAxis: {
          type: 'category',
          data: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
        },
        yAxis: {
          type: 'value',
          name: '总数',
        },
        series: [
          {
            data: [150, 230, 224, 218, 135, 147, 260],
            type: 'line'
          }
        ]
      });
    },
  },
};
</script>
